<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="copyright" content="www.infoearth.com" />
		<title>上报险情</title>
		<link rel="stylesheet" href="../css/api.css">
		<link rel="stylesheet" href="../css/base.css">
		<link rel="stylesheet" type="text/css" href="../css/api.css"/>
		<link rel="stylesheet" href="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.css">
		<script src="../lib/jquery.mobile-1.4.5/jquery.min.js"></script>
		<script src="../lib/jquery.mobile-1.4.5/jquery.mobile-1.4.5.min.js"></script>
		<script type="text/javascript" src="../script/api.js"></script>
		<script src="../lib/config.js"></script>
		<script src="../lib/common.js"></script>
	</head>
	<body>
		<div id="wrap">
			<header id='header'>
				<nav id="nav">
					<a id="left" onclick="api.closeWin();"> <span>返回</span> </a>
					<div id="middle">
						<strong id="title">上报险情</strong>
					</div>
					<a class="rightSave" id="btnSave"><span>保存</span></a>
				</nav>
			</header>
			<div id="main">
				<div style="margin: 10px 15px;">
					<div style="margin: 10px 0px;">
						<label for="tbHazardName">灾害名称：</label>
						<input name="tbHazardName" id="tbHazardName" type="text" readonly="readonly">
					</div>
					<div style="margin: 10px 0px;">
						<label for="tbName">险情名称：</label>
						<input name="tbName" id="tbName" type="text" data-clear-btn="true">
					</div>
					<div style="margin: 10px 0px;">
						<div style="float:left;margin:15px 0px;">
							<label>发生时间：</label>
						</div>
						<div style="float:left;width:120px;">
							<input id="tbDate" type="text" placeholder="日期" readonly="readonly">
						</div>
						<div style="float:left;margin-left:10px;width:120px;">
							<input id="tbTime" type="text" placeholder="时间" readonly="readonly">
						</div>
						<div style="clear:both;"></div>
					</div>
					<div style="margin: 10px 0px;">
						<label for="tbLocation">地理位置：</label>
						<input name="tbLocation" id="tbLocation" type="text" data-clear-btn="true">
					</div>
					<div style="margin: 10px 0px;">
						<label for="tbContent">险情摘要：</label>
						<textarea name="tbContent" id="tbContent" rows="8" cols="40"></textarea>
					</div>
				</div>
				<div style="clear:both;"></div>
				<div style="margin: 20px 15px;">
					<div style="float:right;margin:0px 10px;">
						<a> <img src="../image/picture_add.png" id="btnUploadImage" style="width:32px;height: 32px;"/> </a>
					</div>
					<div style="float:right;margin:0px 10px;">
						<a> <img src="../image/camera_add.png" id="btnCamera"  style="width:32px;height: 32px;"/> </a>
					</div>
					<div style="width:80px;float:right;margin:2px 0px;">
						<label for="name2">上传照片:</label>
					</div>
				</div>
				<div style="clear: both;"></div>
				<div id="divImages" style="margin:10px 10px;"></div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		var hazardInfo = null;
		var position = null;
		var pics = [];
		apiready = function() {
			api.addEventListener({
				name : 'keyback'
			}, function(ret, err) {
				api.closeWin();
			});
			//获取灾害点信息
			hazardInfo = getCookie('hazardInfo');
			hazardInfo = JSON.parse(hazardInfo);
			//设置初始值
			$("#tbHazardName").val(hazardInfo.JBQK0110);
			$("#tbLocation").val(getLocation(hazardInfo));
			$("#tbDate").val(getLocaleDate());
			$("#tbTime").val(getLocaleTime());
			//$("#tbMan").val(getCookie('QcqfMan'));
			bandDatetimeSelEvent();
			initialCamera();
			//获取设备所处位置
			api.getLocation(function(ret, err) {
//				showMsg('经纬度:'+JSON.stringify(ret));
				if (ret.status) {
					position = ret;
				}
			});
			$("#btnSave").click(function() {
				saveRecord();
			});
		}
		
		function getLocation(row) {
			var location = '';
			if (row.JBQK0138 != null)
				location += row.JBQK0138;
			if (row.JBQK0139 != null)
				location += row.JBQK0139;
			if (row.JBQK0140 != null)
				location += row.JBQK0140;
			if (row.JBQK0150 != null)
				location += row.JBQK0150;
			if (row.JBQK0160 != null)
				location += row.JBQK0160;
			return location;
		}
		
		function bandDatetimeSelEvent() {
			$("#tbDate").mousedown(function() {
				api.openWin({
					name : 'DateSelector',
					url : '../html/DateSelector.html'
				});
			});
			$("#tbTime").mousedown(function() {
				api.openWin({
					name : 'TimeSelector',
					url : '../html/TimeSelector.html'
				});
			});
			api.addEventListener({
				name : 'DateInputed'
			}, function(ret) {
				if (ret && ret.value) {
					var value = ret.value;
					$("#tbDate").val(value.key1);
				}
			});
			api.addEventListener({
				name : 'timeInputed'
			}, function(ret) {
				if (ret && ret.value) {
					var value = ret.value;
					$("#tbTime").val(value.key1);
				}
			});
		}

		function saveRecord() {
			Guid2(function(res) {
				var item = {
					"QCQFXQSB01010" : res,
					"QCQFXQSB01020" : hazardInfo.JBQK0100,
					"QCQFXQSB01030" : hazardInfo.JBQK0100,
					"QCQFXQSB01040" : hazardInfo.JBQK0110,
					"QCQFXQSB01050" : $("#tbName").val(),
					"QCQFXQSB01060" : $("#tbDate").val() + " " + $("#tbTime").val(),
					"QCQFXQSB01070" : $("#tbLocation").val(),
					"QCQFXQSB01080" : $("#tbContent").val(),
					"QCQFXQSB01090" : getCookie('QcqfMan'),
					"QCQFXQSB01100" : getCookie('PhoneNum'),
					"QCQFXQSB01110" : getLocaleDatetime(),
					"QCQFXQSB01120" : 0,
					"QCQFXQSB01130" : 0,
					"QCQFXQSB01140" : 0,
					"QCQFXQSB01150" : '0'
				};
				if (pics.length > 0)
					item.QCQFXQSB01150 = '1';
				if (position != null) {
					item.QCQFXQSB01120 = position.longitude;
					item.QCQFXQSB01130 = position.latitude;
				}
				if (item.QCQFXQSB01050 == '') {
					showMsg("请输入险情名称！");
					return;
				}
				showSaveProgress();
				//			item.QCQFXQSB01010 = Guid();
				//				var data = {
				//					"record" : [item]
				//				};
				//				addRecords('qcqf', 'QCQFXQSB01', data, function(response) {
				//					saveDealwithTask(item);
				//					sendMsg2Relation(item);
				//					//保存照片
				//					if (pics.length > 0) {
				//						uploadPictures(pics, item.QCQFXQSB01010, 'qcqfApp');
				//					}
				//					showMsg('保存成功！');
				//				}, showErro, false);
				// showMsg("上报参数"+JSON.stringify(item));
				$.ajax({
					url : ip_req + 'ZHFZ/ZHFZService.svc/SaveQCQFXQSB01',
					type : 'GET',
					dataType : 'jsonp',
					data : {
						parameters : JSON.stringify(item),
					}
				}).done(function(res) {
					showMsg('文件信息保存成功,正在上传,请稍等...');
//					saveDealwithTask(item);
					//					sendMsg2Relation(item);
					//保存照片
					if (pics.length > 0) {
						uploadPictures(pics, item.QCQFXQSB01010, 'qcqfApp');
					}
					
				});
				api.hideProgress();
			});
		}

		//发送短信通知相关人
		function sendMsg2Relation(qxInfo) {
			var mobiles = getMobiles();
			if (mobiles == null || mobiles.length < 1)
				return;
			var now = getServerTime();
			if (now == null) {
				alert("获取服务器时间失败！");
				return;
			}
			var msg = "群测群防险情上报提醒：" + now;
			msg += "，" + qxInfo.QCQFXQSB01090;
			msg += "上报了险情，险情名称：" + qxInfo.QCQFXQSB01050 + "，地理位置：" + qxInfo.QCQFXQSB01070;
			sendMsg(msg, mobiles);
		}

		//获取电话号码
		function getMobiles() {
			var mobiles = [];
			var filter = "SYSLXR01220 = '1'";
			var order = 'SYSLXR01030 asc,SYSLXR01010 asc';
			var record = {
				data : [],
				total : 0
			};
			getRecordsByFilterOrder("qcqf", "SYS_LXR01", filter, order, function(response) {
				var persons = response.record;
				if (persons != null && persons.length > 0) {
					for (var i = 0; i < persons.length; i++) {
						if (persons[i].SYSLXR01060 == null || persons[i].SYSLXR01060 == '')
							continue;
						if (mobiles.indexOf(persons[i].SYSLXR01060) >= 0)
							continue;
						mobiles.push(persons[i].SYSLXR01060);
					}
				}
			}, showErro, false);
			return mobiles;
		}

		//新增上报处理任务记录
		function saveDealwithTask(xqInfo) {
			Guid2(function(res) {
				var data = {
					'YJZSSBCL01010' : res, //记录ID
					'YJZSSBCL01020' : '手机', //来源
					'YJZSSBCL01030' : xqInfo.QCQFXQSB01060, //时间
					'YJZSSBCL01040' : xqInfo.QCQFXQSB01080, //内容
					'YJZSSBCL01050' : xqInfo.QCQFXQSB01030, //灾害点编号
					'YJZSSBCL01060' : '巡查记录', //业务类别
					'YJZSSBCL01070' : xqInfo.QCQFXQSB01010, //业务数据ID
					'YJZSSBCL01080' : getLocaleDatetime(), //系统时间
					'YJZSSBCL01090' : xqInfo.QCQFXQSB01140//备注
				};
				var record = {
					"record" : [data]
				};
				addRecords("qcqf", "YJZSSBCL01", record, function() {
				}, showErro, false);
			});
		}

		function onPicBack(ret, err) {
			if (ret) {
				var img = "<img src='" + ret.data + "' style='width:128px;margin:10px 10px;'/>"
				$("#divImages").append(img);
				pics.push(ret.data);
			} else {
				api.alert({
					msg : err.msg
				});
			}
		}

		function initialCamera() {
			$("#btnUploadImage").click(function() {
				api.getPicture({
					sourceType : 'library',
					encodingType : 'jpg',
					mediaValue : 'pic',
					destinationType : 'url',
					allowEdit : false,
					quality : 50,
					targetWidth : 800,
					targetHeight : 800,
					saveToPhotoAlbum : false
				}, function(ret, err) {
					onPicBack(ret, err);
				});
			});
			$("#btnCamera").click(function() {
				api.getPicture({
					sourceType : 'camera',
					encodingType : 'jpg',
					mediaValue : 'pic'
				}, function(ret, err) {
					onPicBack(ret, err);
				});
			});
		}
	</script>
	</body>
</html>